/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia;

import org.sourceprojects.lycia.exceptions.LyciaParsingException;

/**
 * Interface defining a converter for special datatypes. Basic implementations
 * for primitive- / wrappertypes as well as byte[] and String[] are implemented
 * in internal packages.<br>
 * Implementations of {@link TypeConverter} MUST be threadsafe!<br>
 * Old {@link DataConverter} class is still existing for compatibility reasons
 * but should not be considered for usage in newer implementations since it is
 * just a subclass of {@link TypeConverter}.
 * 
 * @author noctarius
 * @since 1.9.9
 */
public interface TypeConverter {

	/**
	 * Tells the internal parser implementation if the {@link TypeConverter} is
	 * capable of handling the given targetclass.
	 * 
	 * @param type
	 *            Type of targetclass to test
	 * @return true if DataConverter can handle that type of targetclass
	 * @since 1.9.9
	 */
	boolean accept(Class<?> type);

	/**
	 * Converts the given value to a value of classtype given by parameter
	 * "type".<br/>
	 * If some exception occurs while converting it can be just thrown out and
	 * is handled by {@link LyciaParser} implementation automatically as a
	 * {@link LyciaParsingException}.
	 * 
	 * @param <B>
	 *            ContextObject type
	 * @param value
	 *            Value to be converted
	 * @param type
	 *            Targetclass to be converted to
	 * @param parser
	 *            Actual LyciaParser instance
	 * @return The converted value ready to be injected
	 * @throws Exception
	 *             If some exception occurs while parsing it is thrown out
	 * @since 1.9.9
	 */
	<B> Object convert(Object value, Class<?> type, LyciaParser<B> parser)
			throws Exception;

	/**
	 * Converts the given value to a value of classtype given by parameter
	 * "type".<br/>
	 * If some exception occurs while converting it can be just thrown out and
	 * is handled by {@link LyciaParser} implementation automatically as a
	 * {@link LyciaParsingException}.
	 * 
	 * @param value
	 *            Value to be converted
	 * @param type
	 *            Targetclass to be converted to
	 * @return The converted value ready to be injected
	 * @throws Exception
	 *             If some exception occurs while parsing it is thrown out
	 * @since 1.9.1
	 */
	Object convert(Object value, Class<?> type) throws Exception;

}
